# coding:utf8
'''
设置断点，读内存
'''
 
from x64dbgpy.pluginsdk import x64dbg
from x64dbgpy.pluginsdk import memory


import x64dbgpy.pluginsdk._scriptapi.debug as dbg
 
x64dbg.SetBreakpoint(0x04B3094A)
# x64dbg.Run()
# 地址 长度

#x64dbg.DbgScriptCmdExec("msg (1111)") 

#name = memory.Read(0x04B3094A, 0xFF)
#print('name: {}'.format(name))

#x64dbg.GuiAddLogMessage("GuiAddLogMessage xxxx");

x64dbg.Comment_Set(0x032865A2, 'test comment')

module_name = pluginsdk.GetMainModuleName()

print(module_name);

#弹窗口
#pluginsdk.Message("Message")

# 10E011 mainover Rva

module_base = pluginsdk.GetMainModuleBase()


def callback():
    # 读取栈上的参数
    x = pluginsdk.ReadDword(Register.ESP)
    print("Breakpoint callback [ESP]", hex(x))
    
    x = pluginsdk.ReadDword(Register.ESP+4)
    print("Breakpoint callback [ESP+4]", hex(x))
    
    x = pluginsdk.ReadDword(Register.ESP+8)
    print("Breakpoint callback [ESP+8]", hex(x))
    
    # 判断是否有效内存指针
    if pluginsdk.IsValidPtr(Register.EBX):
        print("EBX is a valid pointer", hex(Register.EBX))
    else :
        print("EBX is a bad error pointer", hex(Register.EBX))
        
    if (x>10) :
        print("call hanle > 10, run...");
        #x64dbg.DbgScriptCmdExec("erun") 
        #Run();
    else :
        print("call hanle < 10");
    print("running")
    #dbg.Run()
    #dbg.StepOut()
    # 管用 DbgCmdExec
    pluginsdk.x64dbg.DbgCmdExec("run")
    # 管用 DbgCmdExecDirect
    #pluginsdk.x64dbg.DbgCmdExecDirect('run')
    
    # DbgScriptCmdExec 不好用
    # x64dbg.DbgScriptCmdExec("run") 
    print("running2")


print("start python")
bp_addr = pluginsdk.GetEIP() # Example address
bp_addr = (module_base+0x2E80);
Breakpoint.add(bp_addr,callback)

#Script::Comment::Set(0x032865A2, 'test comment')

# 调用RemoteGetProcAddress函数以获取API地址
api_address = x64dbg.RemoteGetProcAddress("kernel32.dll", "CreateFileW")

# 打印API地址
print("The address of CreateFileW in kernel32.dll is: ", hex(api_address))
print("The address of CreateFileW in kernel32.dll is: 0x{}".format(hex(api_address)))




print("work over ... \n")

x64dbg.Wait()